package ru.agiledev.aos.frontend.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import ru.agiledev.aos.commons.dto.LightWord;
import ru.agiledev.aos.commons.dto.SearchEntry;
import ru.agiledev.aos.commons.dto.SemanticQuery;
import ru.agiledev.aos.core.SemanticEngine;
import ru.agiledev.aos.frontend.client.SearchService;
import ru.agiledev.aos.frontend.shared.SearchResult;
import ru.agiledev.aos.frontend.shared.TransformationResult;

import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class SearchServiceImpl extends RemoteServiceServlet implements
        SearchService {

    @Override
    public TransformationResult getTransformationResults(String query) {
        SemanticEngine engine = new SemanticEngine();

        SemanticQuery sQuery = engine.transformQuery(query);

        String key = UUID.randomUUID().toString();

        QueryRegistry.getInstance().registerQuery(sQuery, key);

        TransformationResult tResult = new TransformationResult();
        tResult.setKey(key);

        //now server-side constructing of result
        tResult.setInitialQuery(sQuery.getInitialQuery());
        if(sQuery.getQuestionType()==null){
            tResult.setQuestionType(null);
            return tResult;
        } else {
            tResult.setQuestionType(sQuery.getQuestionType().getValue());
        }

        if(sQuery.getDistinctiveObject()!=null){
            tResult.setDistinctiveObject(sQuery.getDistinctiveObject().getValue());
        } else {
            tResult.setDistinctiveObject("не определен");
        }


        if((sQuery.getFirstObject()!=null)&&(sQuery.getFirstObject().size()>0)){
            StringBuilder lBuilder = new StringBuilder();
            for (Iterator<LightWord> iterator = sQuery.getFirstObject().iterator(); iterator.hasNext();) {
                LightWord lightWord = iterator.next();
                lBuilder.append(lightWord.getValue());
                lBuilder.append(" ");
            }

            tResult.setFirstObject(lBuilder.toString());

        } else {
            tResult.setFirstObject("не определен");
        }

        if((sQuery.getSecondObject()!=null)&&(sQuery.getSecondObject().size()>0)){
            StringBuilder lBuilder = new StringBuilder();
            for (Iterator<LightWord> iterator = sQuery.getSecondObject().iterator(); iterator.hasNext();) {
                LightWord lightWord = iterator.next();
                lBuilder.append(lightWord.getValue());
                lBuilder.append(" ");
            }
            tResult.setSecondObject(lBuilder.toString());
        }

        if((sQuery.getAdditionalObject()!=null)&&(sQuery.getAdditionalObject().size()>0)){
            StringBuilder lBuilder = new StringBuilder();
            for (Iterator<LightWord> iterator = sQuery.getAdditionalObject().iterator(); iterator.hasNext();) {
                LightWord lightWord = iterator.next();
                lBuilder.append(lightWord.getValue());
                lBuilder.append(" ");
            }

            tResult.setAdditionalObject(lBuilder.toString());
        }


        if((sQuery.getDateObject()!=null)&&(sQuery.getDateObject().size()>0)){
            StringBuilder lBuilder = new StringBuilder();
            for (Iterator<LightWord> iterator = sQuery.getDateObject().iterator(); iterator.hasNext();) {
                LightWord lightWord = iterator.next();
                lBuilder.append(lightWord.getValue());
                lBuilder.append(" ");
            }

            tResult.setDateObject(lBuilder.toString());
        }

        if((sQuery.getQueries()!=null) &&(sQuery.getQueries().size()>0)){
            tResult.setQueries(sQuery.getQueries());
        }

        return tResult;
    }

    @Override
    public List<SearchResult> getSearchResults(String key, String query) {
        SemanticEngine engine = new SemanticEngine();

        List<SearchEntry> results = null;

        if(QueryRegistry.getInstance().contains(key)){
            results = engine.search(QueryRegistry.getInstance().getQuery(key));
        } else {
            SemanticQuery q = engine.transformQuery(query);
            results = engine.search(q);
        }

        return Transformer.transformSearchResult(results);
    }
}
